// ignore_for_file: non_constant_identifier_names

import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:verse_systems/commom/models/order_model.dart';
import 'package:verse_systems/commom/models/product_model.dart';
import 'package:verse_systems/net/http_dio.dart';
import 'package:verse_systems/utils/account_util.dart';
import 'package:verse_systems/pages/order/order_state.dart';

class OderControl extends GetxController {
  final OderState state = OderState();

  getOrderList() async {
    final String token = await AccountUtil.getUserToken();
    Request.postCallback(
      HttpUrls.getOrderList,
      data: {'token': token},
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
        if (result.status == 0) {
          debugPrint("getOrderList ----->$result");

          final List product = result.data["product"];
          final List order_list = result.data["order_list"];
          state.product =
              product.map((json) => ProductModel.fromJson(json)).toList();
          state.order_list =
              order_list.map((json) => OrderModel.fromJson(json)).toList();
          state.no_order_notice =  result.data["no_order_notice"];
          update();
        }
      },
    );
  }




  jumpProductWeb({String? productID}) async {
    final String token = await AccountUtil.getUserToken();
    Map<String, dynamic> map = HashMap();
    map['token'] = token;
    map['id'] = productID;

    Request.postCallback(
      HttpUrls.getProductLink,
      data: map,
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
        if (result.status == 0) {
          String productUrl = result.data["path"];
          if (await canLaunchUrl(Uri.parse(productUrl))) {
            await launchUrl(Uri.parse(productUrl));
          } else {
            throw 'Could not launch $productUrl';
          }
        }
      },
    );
  }

  @override
  void onInit() {
    super.onInit();
    getOrderList();
  }
}
